<?php
session_start();

$marks = &$_SESSION['marks'];

if (!isset($_REQUEST['mark']) || !is_numeric($_REQUEST['mark']))
{
	echo json_encode(array( "error" => "Must be a number.", "field" => "mark" ));
}
if (isset($_FILES['answerfile']) && $_FILES['answerfile']['error'] == 0 && $_FILES['answerfile']['type'] != "text/plain")
{
	echo json_encode(array( "error" => "Must be a text file.", "field" => "answerfile" ));
}
else
{
	$mark = $_REQUEST['mark'];
	$subject_id = $_REQUEST['subject_id'];
	$student_id = $_REQUEST['student_id'];

	if ($mark > 100 or $mark < 0)
	{
		echo json_encode(array( "error" => "Mark must be between 0 and 100.", "field" => "mark" ));
	}
	else
	{
		$id = strval($_SESSION['marks_lastid']+1);
		$marks[$id] = array("mark" => $mark, "subject_id" => $subject_id, "student_id" => $student_id);
		$_SESSION['marks_lastid'] = $id;
		
		if (isset($_FILES['answerfile']) && $_FILES['answerfile']['error'] == 0)
		{
			move_uploaded_file($_FILES['answerfile']['tmp_name'], "answerfiles/".$id.".txt");
		}
		
		echo json_encode(array( "error" => "" ));
	}
}
?>